package clinic.ms.controller;

import clinic.ms.entity.Home;
import clinic.ms.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.List;

@Controller
public class HomeController {
    @Autowired
    HomeService homeService = new HomeService();

    // 添加家庭病房
    @GetMapping("/addHome")
    public String toAddHome() {
        return "/addHome";
    }

    @PostMapping("/addHome")
    public ModelAndView addHome(@RequestParam(value = "homeName") String homeName,
                                @RequestParam(value = "homePhone") String homePhone,
                                @RequestParam(value = "homeAddress") String homeAddress,
                                @RequestParam(value = "homeRemarks") String homeRemarks,
                                @RequestParam(value = "homeRemarks2") String homeRemarks2,
                                @RequestParam(value = "homeRemarks3") String homeRemarks3,
                                @RequestParam(value = "homePer") String homePer) {
        ModelAndView modelAndView = new ModelAndView("/addHome");
        Home home = new Home("#", homeName, homePhone, homeAddress, homeRemarks, homeRemarks2, homeRemarks3, homePer);
        homeService.insertHome(home);
        return modelAndView;
    }

    @GetMapping("/HomeAdmin")
    public ModelAndView toHomeAdmin() {
        ModelAndView modelAndView = new ModelAndView("/HomeAdmin");
        List<Home> homeList = homeService.queryByNoAndName("", "");

        modelAndView.addObject("homeList", homeList);
        return modelAndView;
    }

    @GetMapping("/editHome")
    public ModelAndView getEditHome(@RequestParam("id") String homeNo) {
        ModelAndView modelAndView = new ModelAndView("/editHome");
        Home home = homeService.queryByNo(homeNo);
        modelAndView.addObject("home", home);
        return modelAndView;
    }

    @PostMapping("/editHome")
    public ModelAndView editHome(@RequestParam(value = "homeNo") String homeNo,
                                 @RequestParam(value = "homeName") String homeName,
                                 @RequestParam(value = "homePhone") String homePhone,
                                 @RequestParam(value = "homeAddress") String homeAddress,
                                 @RequestParam(value = "homeRemarks") String homeRemarks,
                                 @RequestParam(value = "homeRemarks2") String homeRemarks2,
                                 @RequestParam(value = "homeRemarks3") String homeRemarks3,
                                 @RequestParam(value = "homePer") String homePer) {
        ModelAndView modelAndView = new ModelAndView("/editHome");
        Home home = new Home(homeNo, homeName, homePhone, homeAddress, homeRemarks, homeRemarks2, homeRemarks3, homePer);
        if (homeService.updateByNo(home))
            modelAndView.addObject("home", homeService.queryByNo(homeNo));
        else modelAndView.addObject("home", home);
        return modelAndView;
    }

    @PostMapping("/deleteHome")
    public ModelAndView deleteHome(String id) {
        homeService.deleteByNo(id);
        return new ModelAndView(new RedirectView("/HomeAdmin"));
    }

    @PostMapping("/searchHome")
    public ModelAndView searchDrug(String no, String name) {
        List<Home> homes;
        //System.out.println("++++"+no+name);
        homes = homeService.queryByNoAndName(no, name);
        ModelAndView modelAndView = new ModelAndView("/HomeAdmin");
        modelAndView.addObject("homeList", homes);
        return modelAndView;
    }
}
